// PassengerTripKey.java
package com.taxi.analysis.passengerTrip;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;

/**
 * 乘客行程复合键类
 * 用于二次排序：先按乘客ID排序，再按行程时间排序
 */
public class PassengerTripKey implements WritableComparable<PassengerTripKey> {

    private String passengerId;      // 乘客ID（主排序键）
    private long pickupTimestamp;    // 上车时间戳（次排序键）
    private int tripSequence;        // 行程序号（辅助排序键）

    // 默认构造函数
    public PassengerTripKey() {
        this.passengerId = "";
        this.pickupTimestamp = 0L;
        this.tripSequence = 0;
    }

    // 带参数构造函数
    public PassengerTripKey(String passengerId, long pickupTimestamp, int tripSequence) {
        this.passengerId = passengerId;
        this.pickupTimestamp = pickupTimestamp;
        this.tripSequence = tripSequence;
    }

    // Getter和Setter方法
    public String getPassengerId() { return passengerId; }
    public void setPassengerId(String passengerId) { this.passengerId = passengerId; }

    public long getPickupTimestamp() { return pickupTimestamp; }
    public void setPickupTimestamp(long pickupTimestamp) { this.pickupTimestamp = pickupTimestamp; }

    public int getTripSequence() { return tripSequence; }
    public void setTripSequence(int tripSequence) { this.tripSequence = tripSequence; }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(passengerId);
        out.writeLong(pickupTimestamp);
        out.writeInt(tripSequence);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        passengerId = in.readUTF();
        pickupTimestamp = in.readLong();
        tripSequence = in.readInt();
    }

    /**
     * 比较方法：实现二次排序逻辑
     * 1. 首先按乘客ID排序
     * 2. 然后按时间戳排序
     * 3. 最后按行程序号排序
     */
    @Override
    public int compareTo(PassengerTripKey other) {
        // 第一级排序：按乘客ID
        int passengerComparison = this.passengerId.compareTo(other.passengerId);
        if (passengerComparison != 0) {
            return passengerComparison;
        }

        // 第二级排序：按时间戳（升序）
        int timeComparison = Long.compare(this.pickupTimestamp, other.pickupTimestamp);
        if (timeComparison != 0) {
            return timeComparison;
        }

        // 第三级排序：按行程序号（升序）
        return Integer.compare(this.tripSequence, other.tripSequence);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;

        PassengerTripKey that = (PassengerTripKey) obj;
        return pickupTimestamp == that.pickupTimestamp &&
                tripSequence == that.tripSequence &&
                passengerId.equals(that.passengerId);
    }

    @Override
    public int hashCode() {
        int result = passengerId.hashCode();
        result = 31 * result + (int) (pickupTimestamp ^ (pickupTimestamp >>> 32));
        result = 31 * result + tripSequence;
        return result;
    }

    @Override
    public String toString() {
        return String.format("PassengerTripKey{passengerId='%s', pickupTimestamp=%d, tripSequence=%d}",
                passengerId, pickupTimestamp, tripSequence);
    }
}
